﻿package Cain.page
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Matrix;
	import gs.TweenMax;
	import gs.easing.*;
	/**
	 * ...
	 * @author Cain@2009 in ADCAST
	 */
	public class Page extends EventDispatcher 
	{
		private var p1:DisplayObject;
		private var p2:DisplayObject;
		private var	inoff:Boolean;
		private var tim:Number;
		private var _pn:Sprite;
		private var p1_a_sp:Sprite;
		private var p1_b_sp:Sprite;
		private var sh_sp:Sprite;
		private var sh_sp2:Sprite;
		public function Page(_p1:DisplayObject,_p2:DisplayObject,_in:Boolean=true,_time:Number=2) 
		{
			p1 = _p1;
			p2 = _p2;
			inoff = _in;
			tim = _time;
			_pn = Sprite(p1.parent);
			init();
		}
		
		private function init():void
		{
			p2_init();
			p1_copy();
			_pn.removeChild(p1);
			
			if (inoff) 
			{
				r_in();
			}else {
				l_in();
			}
		}
		
		private function gra_add():void
		{
			sh_sp = new Sprite();
			sh_sp2 = new Sprite();
			var gra:Sprite = new Sprite;
			var col_arr:Array = new Array(0, 0);
			var alp_arr:Array = new Array(.5, 0);
			var rr_arr:Array = new Array(120, 255);
			gra.graphics.beginGradientFill("linear", col_arr, alp_arr, rr_arr);
			gra.graphics.drawRect(0, 0, p1_a_sp.width , p1_a_sp.height);
			sh_sp.addChild(gra);
			
			var gra2:Sprite = new Sprite;
			var alp2_arr:Array = new Array(.5, 0);
			var rr2_arr:Array = new Array(120, 255);
			gra2.graphics.beginGradientFill("linear", col_arr, alp2_arr, rr2_arr);
			gra2.graphics.drawRect(0, 0, p1_a_sp.width , p1_a_sp.height);
			gra2.rotation = 180;
			gra2.y = p1_a_sp.height;
			sh_sp.addChild(gra2);
			sh_sp2.addChild(sh_sp);
			sh_sp2.x = p1.x + p1_a_sp.width;
			sh_sp2.y = p1.y;
			_pn.addChild(sh_sp2);
		}
		private function l_in():void
		{
			_pn.addChild(p1_a_sp);
			gra_add();
			TweenMax.from(sh_sp,tim/4,{alpha:0,ease:Cubic.easeIn,onComplete:alpha_30});
			TweenMax.to(sh_sp2,tim,{scaleX:0,x:p1.x+p1_a_sp.width*2, ease:Cubic.easeInOut});
			TweenMax.to(p1_a_sp.getChildByName("mk"),tim,{scaleX:0, ease:Cubic.easeInOut});
			TweenMax.to(p1_a_sp, tim, { x: p1.x+p1_b_sp.width*2 , ease:Cubic.easeInOut} );
			TweenMax.to(p1_b_sp.getChildByName("mk"),tim,{scaleX:0, ease:Cubic.easeInOut,onComplete:end_event});
		}
		
		private function alpha_30():void
		{
			TweenMax.to(sh_sp,tim/4,{alpha:.5,ease:Cubic.easeOut,delay:tim/4});
		}
		
		private function end_event():void
		{
			dispatchEvent(new Event("end"));
		}
		private function r_in():void
		{
			gra_add();
			TweenMax.from(sh_sp,tim/4,{alpha:0,ease:Cubic.easeIn,onComplete:alpha_30});
			TweenMax.to(sh_sp2,tim,{scaleX:0,x:p1.x, ease:Cubic.easeInOut});
			TweenMax.to(p1_b_sp.getChildByName("mk"),tim,{scaleX:0, ease:Cubic.easeInOut});
			TweenMax.to(p1_b_sp, tim, { x: p1.x-p1_a_sp.width , ease:Cubic.easeInOut} );
			TweenMax.to(p1_a_sp.getChildByName("mk"),tim,{scaleX:0, ease:Cubic.easeInOut,onComplete:end_event});
		}
		
		private function p1_copy():void
		{
			p1_a_init();
			p1_b_init();
		}
		private function p1_b_init():void
		{
			var p1_b:BitmapData = new BitmapData(p1.width / 2, p1.height);
			var newMatrix:Matrix = new Matrix();
			newMatrix.translate( -p1.width/2, 0);
			p1_b.draw(p1,newMatrix);
			p1_b_sp = new Sprite();
			p1_b_sp.addChild(new Bitmap(p1_b));
			p1_b_sp.x = p1.x+p1.width/2;
			p1_b_sp.y = p1.y;
			_pn.addChild(p1_b_sp);
			addmk_b();
		}
		
		private function addmk_a():void
		{
			var mk:Sprite = new Sprite();
			mk.graphics.beginFill(50, 1);
			mk.graphics.drawRect(0, 0, p1.width / 2, p1.height);
			mk.name = "mk";
			p1_a_sp.addChild(mk);
			p1_a_sp.mask = mk;
		}
		private function addmk_b():void
		{
			var mk:Sprite = new Sprite();
			mk.graphics.beginFill(50, 1);
			mk.graphics.drawRect( -p1.width / 2, 0, p1.width / 2, p1.height );
			mk.name = "mk";
			mk.x = p1.width / 2;
			p1_b_sp.addChild(mk);
			p1_b_sp.mask = mk;
		}
		private function p1_a_init():void
		{
			var p1_a:BitmapData = new BitmapData(p1.width / 2, p1.height);
			p1_a.draw(p1);
			p1_a_sp = new Sprite();
			p1_a_sp.addChild(new Bitmap(p1_a));
			p1_a_sp.x = p1.x;
			p1_a_sp.y = p1.y;
			_pn.addChild(p1_a_sp);
			addmk_a();
		}
		
		private function p2_init():void
		{
			p2.x = p1.x;
			p2.y = p1.y;
			_pn.addChild(p2);
		}
	}
	
}